package com.anyou.mall.goo.goodsComment.service.impl;

import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.goo.goodsComment.domain.GooGoodsCommentAdd;
import com.anyou.mall.goo.goodsComment.mapper.GooGoodsCommentAddMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.goo.goodsComment.mapper.GooGoodsCommentMapper;
import com.anyou.mall.goo.goodsComment.domain.GooGoodsComment;
import com.anyou.mall.goo.goodsComment.service.IGooGoodsCommentService;

/**
 * 商品评价Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-25
 */
@Service
public class GooGoodsCommentServiceImpl implements IGooGoodsCommentService
{
    @Autowired
    private GooGoodsCommentMapper gooGoodsCommentMapper;

    @Autowired
    private GooGoodsCommentAddMapper gooGoodsCommentAddMapper;

    /**
     * 查询商品评价
     *
     * @param goodsCommentId 商品评价主键
     * @return 商品评价
     */
    @Override
    public GooGoodsComment selectGooGoodsCommentByGoodsCommentId(Long goodsCommentId)
    {
        return gooGoodsCommentMapper.selectGooGoodsCommentByGoodsCommentId(goodsCommentId);
    }

    /**
     * 查询商品评价列表
     *
     * @param gooGoodsComment 商品评价
     * @return 商品评价
     */
    @Override
    public List<GooGoodsComment> selectGooGoodsCommentList(GooGoodsComment gooGoodsComment)
    {
        List<GooGoodsComment> gooGoodsComments = gooGoodsCommentMapper.selectGooGoodsCommentList(gooGoodsComment);
          gooGoodsComments.forEach(e->{
             e.setGoodsCommentAdd(gooGoodsCommentAddMapper.selectOne(new LambdaQueryWrapper<GooGoodsCommentAdd>()
                     .eq(GooGoodsCommentAdd::getGoodsCommentId,e.getGoodsCommentId())));
        });

        return gooGoodsComments;
    }

    /**
     * 新增商品评价
     *
     * @param gooGoodsComment 商品评价
     * @return 结果
     */
    @Override
    public int insertGooGoodsComment(GooGoodsComment gooGoodsComment)
    {
        gooGoodsComment.setCreateTime(DateUtils.getNowDate());
        return gooGoodsCommentMapper.insertGooGoodsComment(gooGoodsComment);
    }

    /**
     * 修改商品评价
     *
     * @param gooGoodsComment 商品评价
     * @return 结果
     */
    @Override
    public int updateGooGoodsComment(GooGoodsComment gooGoodsComment)
    {
        gooGoodsComment.setUpdateTime(DateUtils.getNowDate());
        return gooGoodsCommentMapper.updateGooGoodsComment(gooGoodsComment);
    }

    /**
     * 批量删除商品评价
     *
     * @param goodsCommentIds 需要删除的商品评价主键
     * @return 结果
     */
    @Override
    public int deleteGooGoodsCommentByGoodsCommentIds(Long[] goodsCommentIds)
    {
        return gooGoodsCommentMapper.deleteGooGoodsCommentByGoodsCommentIds(goodsCommentIds);
    }

    /**
     * 删除商品评价信息
     *
     * @param goodsCommentId 商品评价主键
     * @return 结果
     */
    @Override
    public int deleteGooGoodsCommentByGoodsCommentId(Long goodsCommentId)
    {
        return gooGoodsCommentMapper.deleteGooGoodsCommentByGoodsCommentId(goodsCommentId);
    }

    /**
     * 商家回复用户评价
     * @param goodsCommentId  评价id
     * @param replyContent    回复内容
     * @return
     */
    @Override
    public ApiResult replyContent(Long goodsCommentId, String replyContent) {
        return ApiResult.success(gooGoodsCommentMapper.replyContent(goodsCommentId, replyContent));
    }

    @Override
    public ApiResult replyContentAdd(Long goodsCommentAddId, String replyContent) {
        gooGoodsCommentAddMapper.update(null,new LambdaUpdateWrapper<GooGoodsCommentAdd>()
                .set(GooGoodsCommentAdd::getReplyContent,replyContent)
                .eq(GooGoodsCommentAdd::getGoodsCommentAddId,goodsCommentAddId));
        return ApiResult.success("回复成功");
    }


}
